#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include "sio0.h"
#include "sio1.h"
#include <avr/interrupt.h>

static int stdio_put(char c, FILE *stream)
{
  (void)stream;  // Tell C compiler that "stream" is unused so we
                 // don't get any warnings.

  output(c);

  return 0;      // 0 return value indicates success
}
static int stdio_get(FILE *stream)
{
  (void)stream;

  return inchar();
}
static FILE mystream = FDEV_SETUP_STREAM(stdio_put, stdio_get, _FDEV_SETUP_RW);

void second_delay(void)
{
	unsigned cycles = 31250;  	// cycles equivalent to 1 second
	
	TCCR1B = _BV(CS12);			// 1:256 prescalar
	TCNT1 = 0;					// counter 1 initialized
	OCR1A = cycles;				// set value
	TIFR1 = _BV(OCF1A);			// set to clear bit

	while ((TIFR1 & _BV(OCF1A)) == 0);	// wait for counter to reach value
}

void main (void)
{
	sio_init0();
	sio_init1();
	sei();

	stdin = stdout = stderr = &mystream;

	// needs to be done 2x. Turns off JTAG interface
	MCUCR |= _BV(JTD);
	MCUCR |= _BV(JTD);
	
	// enable led as output
	DDRC |= _BV(PC6);
	
	char outputMsg[26] = "abcdefghijklmnopqrstuvwxyz";
	char c;
	while (1)
	{
		for (int i=0; i < 26; i++)
		{
			output1(outputMsg[i]);
		}
		
		second_delay();

		while ( ( c = isinput1() ) != 0 )
		{
			//output0( c );
			PORTC ^= _BV( PC6 );
		}

		// toggle LED status
		
	}
	
}
